import { ElMessage, ElMessageBox } from 'element-plus'
import { isMsgOK, isOK } from '@/utils/resUtils'
import { apiList } from '@/api/apiList'
import { ICatalog } from '@/types/keyobject'
import type { TreeOption } from 'naive-ui'
import { repeat } from 'seemly'
import { ref } from 'vue'
import { useSchemaDemo } from '@/hooks'
import { storeToRefs } from 'pinia'
import { useKeyObjectStore } from '@/stores/keyobject'

function useCatalogTree() {
    const {
        airportSchema,
        citySchema,
        peopleSchema,
        plantformSchema,
        transficationSchema,
        weaponSchema,
        dockSchema,
    } = useSchemaDemo

    const emptyCatalog: Partial<ICatalog> = Object.freeze({
        icon: '',
        name: '',
        parent_name: null,
        parent_id: null,
    })

    /* #region********* [edit catalog] **********/

    const editCatalogModel = ref<ICatalog>({
        id: '',
        name: '',
        extra_schema: '',
        children: [],
        parent_name: null,
        icon: '',
        parent_id: null,
    })

    const showEditCatalogDlg = ref(false)
    function handleShowEditCatalog(node: { data: ICatalog }) {
        editCatalogModel.value = { ...node.data }
        showEditCatalogDlg.value = true
    }
    function handleEditCatalog() {
        // request

        apiList.editCatalog(editCatalogModel.value).then((res) => {
            if (isMsgOK(res)) {
                ElMessage.success('保存成功')

                getCatalogList()

                showEditCatalogDlg.value = false
            } else {
                ElMessage.error('保存失败')
            }
        })
    }

    /* #endregion****** [edit catalog] **********/

    /* #region********* [add catalog] **********/

    const addCatalogModel = ref<Partial<ICatalog>>({})

    const treeLoading = ref(false)

    const showAddCatalogDlg = ref(false)

    function handleShowAddCatalog(node: { data: ICatalog }) {
        addCatalogModel.value = { ...emptyCatalog }
        const { id, name, icon } = node.data
        console.log('handleShowAddCatalog data======>', node.data)
        addCatalogModel.value.parent_name = name
        addCatalogModel.value.parent_id = id

        addCatalogModel.value.icon = icon

        showAddCatalogDlg.value = true
    }
    function handleAddCatalog() {
        // do request
        apiList.addCatalog(addCatalogModel.value).then((res) => {
            if (isMsgOK(res)) {
                ElMessage.success('添加成功')
                showAddCatalogDlg.value = false
                getCatalogList()
            } else {
                ElMessage.error('添加失败')
            }
        })
    }

    /* #endregion****** [add catalog] **********/

    /* #region********* [delete catalog] **********/
    function handleDeleteCatalog(node: { data: ICatalog }) {
        ElMessageBox.confirm('确定删掉此分类么?').then((value) => {
            apiList.deleteCatalogs({ id: node.data.id }).then((res) => {
                if (isMsgOK(res)) {
                    ElMessage.success('删除成功')
                    getCatalogList()
                } else {
                    ElMessage.error('删除失败')
                }
            })
        })
    }
    /* #endregion****** [delete catalog] **********/

    /* #region********* [list catalog] **********/

    const tree_data = ref<ICatalog[]>([
        {
            id: 1,
            name: '第一批次',
            value: '1',
            children: [
                {
                    id: 1.1,
                    name: '层级1',
                    value: '1',
                },
                {
                    id: 1.2,
                    name: '层级2',
                    value: '1',
                },
                {
                    id: 1.3,
                    name: '层级3',
                    value: '1',
                },
                {
                    id: 1.4,
                    name: '层级4',
                    value: '1',
                },
                {
                    id: 1.5,
                    name: '层级5',
                    value: '1',
                },
                {
                    id: 1.6,
                    name: '层级6',
                    value: '1',
                },
                {
                    id: 1.7,
                    name: '层级7',
                    value: '1',
                },
                {
                    id: 1.8,
                    name: '层级8',
                    value: '1',
                },
                {
                    id: 1.9,
                    name: '层级9',
                    value: '1',
                },
                {
                    id: 1.10,
                    name: '层级10',
                    value: '1',
                },          {
                    id: 1.11,
                    name: '层级11',
                    value: '1',
                },
                {
                    id: 1.2,
                    name: '层级12',
                    value: '1',
                }
            ]
        }, {
            id: 2,
            name: '第二批次',
            value: '2',
            children: [
                {
                    id: 1.1,
                    name: '层级1',
                    value: '1',
                },
                {
                    id: 1.2,
                    name: '层级2',
                    value: '1',
                },
                {
                    id: 1.3,
                    name: '层级3',
                    value: '1',
                },
                {
                    id: 1.4,
                    name: '层级4',
                    value: '1',
                },
                {
                    id: 1.5,
                    name: '层级5',
                    value: '1',
                },
                {
                    id: 1.6,
                    name: '层级6',
                    value: '1',
                },
                {
                    id: 1.7,
                    name: '层级7',
                    value: '1',
                },
                {
                    id: 1.8,
                    name: '层级8',
                    value: '1',
                },
                {
                    id: 1.9,
                    name: '层级9',
                    value: '1',
                },
                {
                    id: 1.10,
                    name: '层级10',
                    value: '1',
                },          {
                    id: 1.11,
                    name: '层级11',
                    value: '1',
                },
                {
                    id: 1.2,
                    name: '层级12',
                    value: '1',
                }
            ]
        }, {
            id: 3,
            name: '第三批次',
            value: '2',
            children: [
                {
                    id: 1.1,
                    name: '层级1',
                    value: '1',
                },
                {
                    id: 1.2,
                    name: '层级2',
                    value: '1',
                },
                {
                    id: 1.3,
                    name: '层级3',
                    value: '1',
                },
                {
                    id: 1.4,
                    name: '层级4',
                    value: '1',
                },
                {
                    id: 1.5,
                    name: '层级5',
                    value: '1',
                },
                {
                    id: 1.6,
                    name: '层级6',
                    value: '1',
                },
                {
                    id: 1.7,
                    name: '层级7',
                    value: '1',
                },
                {
                    id: 1.8,
                    name: '层级8',
                    value: '1',
                },
                {
                    id: 1.9,
                    name: '层级9',
                    value: '1',
                },
                {
                    id: 1.10,
                    name: '层级10',
                    value: '1',
                },          {
                    id: 1.11,
                    name: '层级11',
                    value: '1',
                },
                {
                    id: 1.2,
                    name: '层级12',
                    value: '1',
                }
            ]
        }
    ])

    async function getCatalogList() {
        treeLoading.value = true
        try {
            const res = await apiList.queryCatalogs({})

            if (isOK(res)) {
                tree_data.value = [res.result!]

                return res.result
            }
        } catch (error) {
            console.warn('请求失败')
        } finally {
            treeLoading.value = false
        }
    }

    /* #endregion****** [list catalog] **********/

    const { curCatalog, curCatalogId, curCatalogName, curSchema } = storeToRefs(
        useKeyObjectStore(),
    )

    function handleTreeClick(data: ICatalog) {
        console.log('handleTreeClick data======>', data)

        curCatalog.value = data

        if (data.hdrs?.fields) {
            curSchema.value = data.hdrs?.fields
        } else {
            curSchema.value = []
        }
    }

    async function handleSubmitSchema(json: string, option: any) {
        console.log('handleSubmitSchema')
        console.log('json', json)
        console.log('option', option)

        const catalog = curCatalog.value!

        const res = await apiList.catalogApi.editCatalog({
            ...catalog,
            hdrs: {
                fields: json,
                options: {
                    resetBtn: {
                        innerText: '重置',
                        show: false,
                    },
                    submitBtn: {
                        innerText: '提交',
                        show: true,
                    },
                    form: {
                        inlineMessage: false,
                        size: 'small',
                        labelPosition: 'right',
                        showMessage: true,
                        labelWidth: '125px',
                        hideRequiredAsterisk: false,
                    },
                },
            },
        })
        if (isMsgOK(res)) {
            ElMessage.success('保存成功')
            getCatalogList()
        }
    }

    return {
        tree_data,
        // add
        addCatalogModel,
        handleAddCatalog,
        showAddCatalogDlg,
        handleShowAddCatalog,
        // delete
        handleDeleteCatalog,

        // edit
        handleEditCatalog,
        editCatalogModel,
        showEditCatalogDlg,
        handleShowEditCatalog,
        // list
        getCatalogList,
        treeLoading,

        // tree
        handleTreeClick,
        curCatalog,
        curCatalogId,
        curCatalogName,

        // schema
        handleSubmitSchema,
        curSchema,
    }
}

export default useCatalogTree
